package com.fang.sort;

import java.util.Comparator;

import com.fang.base.utils.SwapUtils;

public class SelectedSort {
	public static void main(String[] args) {
		Integer[] sort = { 2, 5, 45, 87, 1, 254, 58 };

		SelectedSort selectedSort = new SelectedSort();
		Integer[] data = selectedSort.sort(sort, new Comparator<Integer>() {

			@Override
			public int compare(Integer data1, Integer data2) {
				if (data1 < data2)
					return 1;
				if (data1 > data2)
					return -1;
				return 0;
			}
		});
		for (Integer integer : data) {
			System.out.print(integer + "   ");
		}
	}

	public <T> T[] sort(T[] data, Comparator<? super T> comparator) {
		if (data == null || comparator == null)
			return data;
		int length = data.length;
		int k = 0;
		for (int i = length - 1; i >= 0; i--) {
			k = 0;
			for (int j = 0; j < i; j++) {
				if (comparator.compare(data[j], data[j + 1]) < 0) {
					k = j;
				}
			}
			SwapUtils.swap(data, i, k);
		}
		return data;
	}

}
